<?php 
// Nguyen Binh
// suongmumc@gmail.com
// update 04/2009

defined( '_VALID_NVB' ) or die( 'Direct Access to this location is not allowed.' );
$tpl=new TemplatePower("skin/sk_catif.htm");
$tpl->prepare();

//header
$title="Quản lý trang nội dung";
$root="Root";
$par_page="sr_catif";
$cat_name       ="danh m&#7909;c";
$category       ="catif".$lang;
$id_category    ="id_catif";
$item_name      ="bài viết";
$item           ="info".$lang;
$id_item        ="id_info";
$maxpage=8;
$maxitem=20;


$id=intval($_GET['id']);

// cat id
if($_GET['pid'])
{
	$pid=$_GET['pid'];
}
elseif($_POST['pid'])
{
	$pid=$_POST['pid'];
}
else
{
	$pid=0;
}

// numpage cat
if($_GET['p'])
{
	$p=$_GET['p'];
}
elseif($_POST['p'])
{
	$p=$_POST['p'];
}
//numpage item
if($_GET['pitem'])
{
	$pitem=$_GET['pitem'];
}
elseif($_POST['pitem'])
{
	$pitem=$_POST['pitem'];
}

$id=intval($_REQUEST['id']);

// global 
$tpl->assignGlobal('title',$title);
$tpl->assignGlobal('cat_name',$cat_name);
$tpl->assignGlobal('item_name',$item_name);

$tpl->assignGlobal('p',$p);
$tpl->assignGlobal('pitem',$pitem);
$tpl->assignGlobal('pid',$pid);
$tpl->assignGlobal('par_page',$par_page);
// path page
$navigator="<a href='?page=".$par_page."&code=00'>$root</a>";
$ct=new class_tree($pid);
$ct->get_cat_tree(0,$category,$id_category);	
$ct->get_cat_string_admin($pid,$par_page,$category,$id_category);
$navigator.=$catstring2;								
$tpl->assignGlobal('pathpage',$navigator);
if(!$_GET['code'] || $_GET['code']=='00'){
	show_list_category();
	show_list_item();
}
// show form addnew cat
if($_GET['code']=='01'){
	$tpl->newBlock("addnew_cat");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$tpl->assign("thu_tu",auto_order($category,'thu_tu',$pid));
	show_list_category();
}
// insert new cat
if($_GET['code']=='02'){
	$in_name=compile_post('name');
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['active']		=intval(compile_post("active"));
		$a['parentid']		=intval(compile_post("parentid"));
		
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO ".$category." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		if($DB){
			write_link();
			 message("Th&#234;m m&#7899;i $item_name th&#224;nh c&#244;ng !");
		}
	}
	show_list_category();
}
// show form update cat
if($_GET['code']=='03'){
	$tpl->newBlock("update_cat");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	
	$sql="SELECT * FROM $category WHERE $id_category=$id";
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",$rs['name']);
		$tpl->assign("thu_tu",intval($rs['thu_tu']));
		if($rs['active']==1) $tpl->assign("active","checked");
		$tpl->assign("id",$rs[$id_category]);
	}
	
	show_list_category();
}
// update cat
if($_GET['code']=='04'){
	$in_name=compile_post('name');
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['active']		=intval(compile_post("active"));
		$a['parentid']		=intval(compile_post("parentid"));
		
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE ".$category." SET ".$b." WHERE ".$id_category."=".$id;
		$DB->query($sql);
		if ($DB){
			write_link();
			 message("Sửa chữa thành công !");
		}
	}
	show_list_category();
	show_list_item();

}
// change order active
if($_GET['code']=='05'){
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu DESC, name ASC";
	$db=phan_trang($p,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'p');

	change_active_order_paging($category,$id_category,$db['db']);
	show_list_category();
	show_list_item();

}
// delete category
if($_GET['code']=='06'){
	if(checkDelCat($id)){
		$sql="SELECT * FROM $category WHERE $id_category=".$id;
		$db=$DB->query($sql);
		if($rs=mysql_fetch_array($db)){
			$sql1="DELETE FROM $category WHERE $id_category=".$id;
			
			$db=$DB->query($sql1);
			if($db) {
				write_link();
				message("Đã xóa xong !");
			}
			else{
				 message("Lỗi không xóa được");
			}
		}
	}
	show_list_category();
	show_list_item();

}
// show form addnew item
if($_GET['code']=='11'){
	$tpl->newBlock("addnew_item");
	
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	//----------
	$oFCKeditor = new FCKeditor("content") ;
	$oFCKeditor->BasePath = '../FCKeditor/';
	$oFCKeditor->Value = $rs['content'];
	$tpl->assign("content",$oFCKeditor->CreateHtml());
	
	$tpl->assign("thu_tu",auto_orders($item,'thu_tu',$pid,$id_category));
	
	show_list_item();
}
// insert new item 
if($_GET['code']=='12'){
	$in_name=compile_post('name');
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['active']		=intval(compile_post("active"));
		$a['inhome']		=intval(compile_post("inhome"));
		$a[$id_category]	=intval(compile_post("parentid"));
		$a['content']		=$_POST['content'];
		$a['ngay_dang']		=time()+$CONFIG['time_offset'];
		$a['id_user']=$my['id'];
		
	}
	$b=$DB->compile_db_insert_string($a);
	$sql="INSERT INTO ".$item." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
	$DB->query($sql);
	if($DB) message("Th&#234;m m&#7899;i $item_name th&#224;nh c&#244;ng !");
	show_list_item();
}
// show form update item
if($_GET['code']=='13'){
	$tpl->newBlock("update_item");
	$tpl->assign("id",$id);
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	
	$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		$tpl->assign("name",$rs['name']);
		//----------
		$oFCKeditor = new FCKeditor("content") ;
		$oFCKeditor->BasePath = '../FCKeditor/';
		$oFCKeditor->Value = $rs['content'];
		$tpl->assign("content",$oFCKeditor->CreateHtml());
		
		$tpl->assign("thu_tu",$rs['thu_tu']);
		if($rs['active']==1) $tpl->assign("active","checked");
		if($rs['inhome']==1) $tpl->assign("inhome","checked");
	}
	show_list_item();
}
// update item
if($_GET['code']=='14'){
	$in_name=	compile_post("name");
	if($in_name){
		$a=array();
		$a['name']			=compile_post("name");
		$a['thu_tu']		=intval(compile_post("thu_tu"));
		$a['active']		=intval(compile_post("active"));
		$a['inhome']		=intval(compile_post("inhome"));
		$a[$id_category]	=intval(compile_post("parentid"));
		$a['content']		=$_POST['content'];
		$a['ngay_dang']		=time()+$CONFIG['time_offset'];
		
		$b=$DB->compile_db_update_string($a);
		$sql="UPDATE ".$item." SET ".$b." WHERE ".$id_item."=".$id;
		$db=$DB->query($sql);
		if($db) message("Sửa chữa thành công !");
		else message("Lỗi không sửa được !");
	}
	show_list_item();
}
// change order active - order
if($_GET['code']=='15'){
	$sql="SELECT ".$item.".*,users.name as user_name, users.username as username FROM $item LEFT JOIN users ON(".$item.".id_user=users.id_users) WHERE $id_category=$pid ORDER BY thu_tu DESC, $id_item DESC";
	$db=phan_trang($pitem,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'pitem');
	change_active_order_paging($item,$id_item,$db['db']);
	show_list_category();
	show_list_item();
}
// delete item 
if($_GET['code']=='16'){
	$sql="SELECT * FROM $item WHERE $id_item=".$id;
	$db=$DB->query($sql);
	if($rs=mysql_fetch_array($db)){
		deleteimage($rs['image'],$rs['normal_image'],$rs['small_image']);

		$sql1="DELETE FROM $item WHERE $id_item=".$rs[$id_item];
		$db=$DB->query($sql1);
		if($db) message("Đã xóa xong !");
		else message("Lỗi không xóa được !");
	}
	show_list_category();
	show_list_item();
}
// delete multi item
if($_GET['code']=='17'){
	$id_items=compile_post("del1_id");
	$sql1="SELECT * FROM $item WHERE $id_item IN".$id_items;
	$db1=$DB->query($sql1);
	while($rs1=mysql_fetch_array($db1)){
		deleteimage($rs1['image'],$rs1['normal_image'],$rs1['small_image']);
	}
	$sql="DELETE FROM $item WHERE $id_item IN".$id_items."";
	$db=$DB->query($sql);
	if($db) message("Đã xóa xong !");
	else message("Lỗi không xóa được !");
	
	show_list_category();
	show_list_item();
}
// search item
if($_GET['code']=='18'){
	$keyword=compile_post("keyword");
	$parentid=intval(compile_post("parentid"));
	$dk=" $id_category IN(".Get_ID_Cat($parentid).") AND $item.name like'%".$keyword."%' ";
	 show_list_item($dk);
}

// show list category and item
function show_list_category(){
	global $DB,$tpl,$category,$id_category,$item,$id_item,$pid,$p,$pitem,$maxpage,$maxitem,$par_page;
	$tpl->newBlock("show_category");
	$sql="SELECT * FROM $category WHERE parentid=$pid ORDER BY thu_tu DESC, name ASC";
	$db=phan_trang($p,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'p');
	$i=0;
	$tpl->assignglobal("ncat",mysql_num_rows($db['db']));
	while($rs=mysql_fetch_array($db['db'])){
		$tpl->newBlock("list_cat");
		$i++;
		$tpl->assign("i",$i);
		$tpl->assign("name",$rs['name']);
		$tpl->assign("thu_tu",intval($rs['thu_tu']));
		if($rs['active']==1){
			$tpl->assign("active","checked");
		}
		$tpl->assign("id_category",$rs[$id_category]);
		$tpl->assign("linkcat","?page=$par_page&pid=".$rs[$id_category]);
		$tpl->assign("link_update","?page=$par_page&pid=$pid&id=$rs[$id_category]&code=03&p=$p&pitem=$pitem");
		$tpl->assign("link_del","?page=$par_page&pid=$pid&id=$rs[$id_category]&code=06&p=$p&pitem=$pitem");
	}
	$tpl->assignGlobal("page_cat",$db['cac_trang']);
}
// show list item
function show_list_item($dk=''){
	global $DB,$tpl,$category,$id_category,$item,$id_item,$pid,$p,$pitem,$maxpage,$maxitem,$par_page,$tree;
	$tpl->newBlock("show_item");
	$info['parentid1']=$pid;
	$info['parentid'] .= '<select name="parentid" style="WIDTH: 220px" >';
	$info['parentid'] .= '<option value="0">Root</option>';
	if($tree)
	foreach($tree as $k => $v) {
		foreach($v as $i => $j) {
			$selectstr='';
			if ($info['parentid1']==$k)
				$selectstr=" selected ";
			$info['parentid'] .= '<option value="' . $k . '"'.$selectstr.'>' . $j . '</option>';
		}
	}
	if($dk==''){
		$dk="$id_category=$pid";
	}
	$info['parentid'] .= '</select>';
	$tpl->assign("parentid",$info['parentid']);
	$sql="SELECT ".$item.".*,users.name as user_name, users.username as username FROM $item LEFT JOIN users ON(".$item.".id_user=users.id_users) WHERE  $dk ORDER BY thu_tu DESC, $id_item DESC";

	$db=phan_trang($pitem,"?page=$par_page&pid=$pid",$sql,$maxpage,$maxitem,'pitem');
	$i=0;
	$tpl->assignGlobal("nitem",mysql_num_rows($db['db']));
	while($rs=mysql_fetch_array($db['db'])){
		$tpl->newBlock("list_item");
		$i++;
		$tpl->assign("name",$rs['name']);

		$tpl->assign("i",$i);
		$tpl->assign("id",$rs[$id_item]);
		$tpl->assign("thu_tu",intval($rs['thu_tu']));
		if($rs['active']==1){
			$tpl->assign("active","checked");
		}
		$tpl->assign("user_name",$rs['user_name']);
		$tpl->assign("username",$rs['username']);
		$tpl->assign("creatdate",date('d/m/Y H:s',$rs['ngay_dang']));
		$tpl->assign("link_edit","?page=$par_page&pid=$pid&id=$rs[$id_item]&code=13&pitem=$pitem&p=$p");
	}
	$tpl->assignGlobal("pages_items",$db['cac_trang']);
}

function checkDelCat($id)
{
	global $category,$cat_name,$item_name,$item,$id_category,$DB;
	$sql="select * from ".$category." where parentid = ".$id;
	$db=$DB->query($sql);	
	if(mysql_num_rows($db)>0)
	{
		message("B&#7841;n kh&#244;ng th&#7875; x&#243;a $cat_name n&#224;y v&#236; v&#7851;n c&#242;n $cat_name con b&#234;n trong.h&#227;y x&#243;a c&#225;c $cat_name con tr&#432;&#7899;c!");
		return false;
	}
	else
	{
		$sql1="select * from ".$item." where ".$id_category." = ".$id;
		$db1=$DB->query($sql1);	
		if(mysql_num_rows($db1)>0)
		{
			message("B&#7841;n kh&#244;ng th&#7875; x&#243;a $cat_name n&#224;y v&#236; v&#7851;n c&#242;n $item_name  b&#234;n trong.h&#227;y x&#243;a c&#225;c $item_name con tr&#432;&#7899;c!");
			return false;
		}
		else
		{
			return true;
		}
	}
}
function Get_ID_Cat($pid=0){
	global $DB,$category,$id_category;
	$sql="SELECT * FROM $category WHERE parentid=".$pid;
	$db=$DB->query($sql);
	$list_id=$pid;
	while($rs=mysql_fetch_array($db)){
		$list_id.=",".Get_ID_Cat($rs[$id_category]);
	}
	return $list_id;
}

$tpl->printToScreen();
?>